package com.example.jussi.myproject;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ShowCard extends AppCompatActivity {
    CardBean card;
    SQLiteDatabase database;
    TextView businessName, name, job, email, phoneNum, qqNum, info, addr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_card);
        database = new MyDatabase(this).getWritableDatabase();
        businessName = (TextView) findViewById(R.id.business);
        name = (TextView) findViewById(R.id.name);
        job = (TextView) findViewById(R.id.job);
        email = (TextView) findViewById(R.id.email);
        phoneNum = (TextView) findViewById(R.id.phoneNumber);
        qqNum = (TextView) findViewById(R.id.QQNumber);
        info = (TextView) findViewById(R.id.info);
        addr = (TextView) findViewById(R.id.address);
        initView();
    }

    private void initView() {
        Intent data = getIntent();
        card = (CardBean) data.getSerializableExtra("card");
        businessName.setText(card.getBusinessName());
        name.setText(card.getName());
        job.setText(card.getJob());
        email.setText(card.getEmail());
        phoneNum.setText(card.getPhoneNum());
        qqNum.setText(card.getQqNum());
        info.setText(card.getInfo());
        addr.setText(card.getAddr());
    }

    public void update(View view) {
        Intent intent = new Intent(this, UpdateCard.class);
        intent.putExtra("card", card);
        startActivityForResult(intent, 1);
    }

    public void call(View view) {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, 1);
            return;
        }
        call();
    }

    public void add(View view){
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("http://qm.qq.com/cgi-bin/qm/qr?k=" + card.getQqNum()));
        startActivity(intent);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    card = (CardBean) data.getSerializableExtra("card");
                    String sql = "update cards set business_name=?, name=?, job=?, email=?, phone=?, qqnumber=?, info=?, address=? where id=?";
                    try {
                        database.execSQL(sql, new Object[]{
                                card.getBusinessName(),
                                card.getName(),
                                card.getJob(),
                                card.getEmail(),
                                card.getPhoneNum(),
                                card.getQqNum(),
                                card.getInfo(),
                                card.getAddr(),
                                card.getId()
                        });
                        Toast.makeText(this, "名片修改成功！！！", Toast.LENGTH_SHORT).show();
                        finish();
                    } catch (SQLException e) {
                        e.printStackTrace();
                        Toast.makeText(this, "名片修改失败！！！", Toast.LENGTH_SHORT).show();
                    }
                }else{
                    Toast.makeText(this, "您取消了本次操作！！！", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode){
            case 1:
                if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    call();
                }else{
                    Toast.makeText(this, "You denied the permission.", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
        }
    }

    private void call() {
        try {
            Intent intent = new Intent(Intent.ACTION_CALL);
            intent.setData(Uri.parse("tel:" + card.getPhoneNum()));
            startActivity(intent);
        } catch (SecurityException e) {
            e.printStackTrace();
        }
    }
}
